//
//  MPVolumeViewExtension.swift
//  XiaoweHearingAid
//
//  Created by edy on 2025/6/10.
//

import Foundation
import MediaPlayer

public extension MPVolumeView {
    
    static func setVolume(_ volume: Float) {
        let volumeView = MPVolumeView(frame: .zero)
        volumeView.clipsToBounds = true
//        let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider
//        
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
//            slider?.value = volume
//        }
        volumeView.setVolume(volume)
    }
    
    func setVolume(_ volume: Float) {
        guard let slider = self.subviews.first(where: { $0 is UISlider }) as? UISlider else {
            return
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) { [weak slider] in
            slider?.value = volume
        }
    }
    
}
